package communication.common.shell;

import communication.common.business.base.FcrAutoConfig;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.shell.ShellService;
import io.vertx.ext.shell.ShellServiceOptions;
import io.vertx.ext.shell.term.SSHTermOptions;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.net.URL;

/**
 * 开启一个ssh的shell
 * @author mac
 */
@Slf4j
@NoArgsConstructor
public class FcrServerShell {

    @Autowired
    private FcrAutoConfig fcrAutoConfig;

    @Autowired
    private Vertx vertx;

    /**
     * 启动ssh的shell
     * @return Future
     */
    public Future startShellService(){
        System.out.println(" -----  starting shell service ------- ");
        ShellService service = ShellService.create(vertx,
                new ShellServiceOptions()
                        .setSSHOptions(
                                new SSHTermOptions()
                                        .setHost("localhost")
                                        .setPort(fcrAutoConfig.getSshPort())
                                        .setKeyPairOptions(new JksOptions()
                                                // 密钥必须放在 java -jar 命令所在的工作目录
                                                .setPath("jwt.jks")
                                                // 打开密钥需要的密码
                                                .setPassword("123456")
                                        )
                                        .setAuthOptions(
                                                new JsonObject()
                                                        .put("provider", "shiro")
                                                        .put("type", "PROPERTIES")
                                                        .put("config", new JsonObject()
                                                                .put("properties_path","classpath:auth.properties")
                                                        )
                                        )
                        )
        );
        return service.start();
    }
}
